﻿using System;
using System.Linq;
using Newtonsoft.Json;
using BCCommon;
using BCEntity.Common.Area;
using BCData.Common.Area;
using BCDto.Common.Area;

namespace BCService.Common.Area
{
    /// <summary>
    /// 区域信息业务处理类
    /// </summary>
    public class ZoneService : IZoneService
    {
        private readonly IProvinceData provinceData;
        private readonly ICityData cityData;
        private readonly ICountyData countyData;

        public ZoneService(IProvinceData provinceData,
            ICityData cityData,
            ICountyData countyData)
        {
            this.provinceData = provinceData;
            this.cityData = cityData;
            this.countyData = countyData;
        }

        #region 检查地址
        ///// <summary>
        ///// 检查地址
        ///// </summary>
        ///// <param name="current">当前登录用户</param>
        ///// <param name="dto">基本订单请求对象</param>
        ///// <returns></returns>
        //public OrderZoneEntity CheckAddress(UserDto current, BaseOrderRequestDto dto)
        //{
        //    ProvinceEntity fromProvince = null;
        //    ProvinceEntity toProvince = null;
        //    CityEntity fromCity = null;
        //    CityEntity toCity = null;
        //    CountyEntity fromCounty = null;
        //    CountyEntity toCounty = null;

        //    PerformanceTracer.InvokeAll(
        //        delegate { fromProvince = this.provinceData.GetProvince(dto.From.ZoneCode.Substring(0, 2) + "0000"); },
        //        delegate { fromCity = this.cityData.GetCity(FixedCityCode.GetFixedCityCode(dto.From.ZoneCode)); },
        //        delegate { fromCounty = this.countyData.GetCounty(dto.From.ZoneCode); },
        //        delegate { toProvince = this.provinceData.GetProvince(dto.To.ZoneCode.Substring(0, 2) + "0000"); },
        //        delegate { toCity = this.cityData.GetCity(FixedCityCode.GetFixedCityCode(dto.To.ZoneCode)); },
        //        delegate { toCounty = this.countyData.GetCounty(dto.To.ZoneCode); });
            
        //    if (fromProvince == null || toProvince == null || fromCity == null || toCity == null|| fromCounty==null || toCounty==null)
        //    {
        //        this.noFreightLogData.Add(current.UserId, JsonConvert.SerializeObject(dto));
        //        throw new ArgumentException("无法获取城市运费，可能原因城市编码存在错误。");

        //    }

        //    //if (fromCounty == null)
        //    //{
        //    //    var list = this.countyData.GetCounty(fromProvince.ProvinceName, fromCity.CityName);
        //    //    if (list.Any())
        //    //    {
        //    //        string address = dto.From.StreetAddress.Replace($"{fromProvince.ProvinceName}{fromCity.CityName}", "");
        //    //        fromCounty = list.Where(p => address.StartsWith(p.CountyName)).FirstOrDefault();
        //    //    }
        //    //    if (fromCounty == null)
        //    //    {
        //    //        throw new ArgumentException("地址信息不正确。");
        //    //    }
        //    //}

        //    //if (toCounty == null)
        //    //{
        //    //    var list = this.countyData.GetCounty(toProvince.ProvinceName, toCity.CityName);
        //    //    if (list.Any())
        //    //    {
        //    //        string address = dto.To.StreetAddress.Replace($"{toProvince.ProvinceName}{toCity.CityName}", "");
        //    //        toCounty = list.Where(p => address.StartsWith(p.CountyName)).FirstOrDefault();
        //    //    }
        //    //    if (toCounty == null)
        //    //    {
        //    //        throw new ArgumentException("地址信息不正确。");
        //    //    }
        //    //}
        //    return new OrderZoneEntity()
        //    {
        //        fromZoneEntity = new ZoneEntity()
        //        {
        //            provinceEntity = fromProvince,
        //            cityEntity = fromCity,
        //            countyEntity = fromCounty
        //        },
        //        toZoneEntity = new ZoneEntity()
        //        {
        //            provinceEntity = toProvince,
        //            cityEntity = toCity,
        //            countyEntity = toCounty
        //        }
        //    };
        //}

        ///// <summary>
        ///// 转换为ZoneEntity
        ///// </summary>
        ///// <param name="dto">订单地址请求对象</param>
        ///// <returns></returns>
        //public ZoneEntity ConvertZoneEntity(OrderAddressRequestDto dto)
        //{
        //    ProvinceEntity province = null;
        //    CityEntity city = null;
        //    CountyEntity county = null;
        //    PerformanceTracer.InvokeAll(
        //        delegate { province = this.provinceData.GetProvince(dto.ZoneCode.Substring(0, 2) + "0000"); },
        //        delegate { city = this.cityData.GetCity(FixedCityCode.GetFixedCityCode(dto.ZoneCode)); },
        //        delegate { county = this.countyData.GetCounty(dto.ZoneCode); });
        //    return new ZoneEntity()
        //    {
        //        provinceEntity = province,
        //        cityEntity = city,
        //        countyEntity = county
        //    };
        //}
       
        #endregion


        public ZoneDto[] GetProvinces()
        {
            return this.provinceData.GetProvinces().Select(p => new ZoneDto
            {
                ZoneId = p.ProvinceCode,
                Name = p.ProvinceName
            }).OrderBy(p => p.ZoneId).ToArray();
        }

        public ZoneDto[] GetCities(string provinceCode)
        {
            return this.cityData.GetCommunities(provinceCode).Select(p => new ZoneDto
            {
                ZoneId = p.CityCode,
                Name = p.CityName
            }).OrderBy(p => p.ZoneId).ToArray();
        }

        public ZoneDto[] GetCounties(string cityCode)
        {
            return this.countyData.GetCounties(cityCode).Select(p => new ZoneDto
            {
                ZoneId = p.CountyCode,
                Name = p.CountyName,
                IsDowntown = p.IsDowntown
            }).OrderBy(p => p.ZoneId).ToArray();
        }

        /// <summary>
        /// 检查地址
        /// </summary>
        /// <param name="zoneCode">基本订单请求对象</param>
        /// <returns></returns>
        public ZoneEntity GetFromZoneEntity(string zoneCode)
        {
            ProvinceEntity fromProvince = null;
            CityEntity fromCity = null;
            CountyEntity fromCounty = null;


            PerformanceTracer.InvokeAll(
                delegate { fromProvince = this.provinceData.GetProvince(zoneCode.Substring(0, 2) + "0000"); },
                delegate { fromCity = this.cityData.GetCity(FixedCityCode.GetFixedCityCode(zoneCode)); },
                delegate { fromCounty = this.countyData.GetCounty(zoneCode); });

            return new ZoneEntity()
            {
                provinceEntity = fromProvince,
                cityEntity = fromCity,
                countyEntity = fromCounty
            };
        }
    }
}
